﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Dynamic;

namespace Props.PublicWeb
{
    public class ConfigSettings : DynamicObject
    {
        private static ConfigSettings current;

        /// <summary>
        /// Gets or sets the current ConfigSettings object.
        /// </summary>
        /// <value>The current.</value>
        public static dynamic Current
        {
            get
            {
                if (current == null)
                {
                    current = new ConfigSettings();
                }
                return current;
            }
            set
            {
                current = value;
            }
        }

        private IDictionary<string, string> settings;

        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigSettings"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        public ConfigSettings(IDictionary<string, string> settings = null)
        {
            if (settings == null)
                LoadSettingsFromConfigFile();
            else
                this.settings = settings;
        }

        private void LoadSettingsFromConfigFile()
        {
            ConfigurationManager.RefreshSection("appSettings");

            this.settings = new Dictionary<string, string>();

            foreach (var key in ConfigurationManager.AppSettings.AllKeys)
                settings.Add(key, ConfigurationManager.AppSettings[key]);

        }

        /// <summary>
        /// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
        /// </summary>
        /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
        /// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result"/>.</param>
        /// <returns>
        /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)
        /// </returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            var key = binder.Name;

            if (settings.ContainsKey(key))
            {
                result = settings[key];
                return true;
            }

            result = null;
            return true;
        }
    }

}
